package hebClustering.vectorSpace;

import hebClustering.documentTypes.Document;
import hebClustering.nlp.Corpus;
import hebClustering.nlp.Token;
import hebClustering.searchEngines.WebsiteInformation;
import hebClustering.weights.IWeightFunc;

import java.util.Set;

/**
 * 
 * A sparse vector that represents a document.
 * 
 */
public class DocumentVector extends SparseVector {

	private WebsiteInformation info;
	
	/**
	 * Creates a new vector, and fills it with elements from the document.<br>
	 * Element i is a calculation of the weight function over the document and the i-th word in the corpus.
	 *  
	 * @param doc - The document that this vector represent.
	 * @param weight - The weight function to be used.
	 * @param dimension - The dimension of the vector.
	 * 
	 * @see Corpus
	 */
	public DocumentVector(Document doc,IWeightFunc weight,int dimension){
		super(dimension);
		
		Set<String> tokensInDoc = doc.getWordSet();
		info = doc.getInfo();
		for(String strTok : tokensInDoc){
			Token tok = Corpus.getCorpus().getToken(strTok);
			addToVector(tok.getId(), weight.calc(tok,doc));
		}
	}
	
	/**
	 * Returns the path of the document this vector represents.
	 * 
	 * @return The path.
	 */
	public String getPath(){
		return info.getUrl();
	}
	
	public void show(){
		System.out.println(info.getUrl());
		for (int j = 0; j < Corpus.getCorpus().getNumOfTokens(); j++) 
            System.out.printf("%9.4f ", ithValue(j));
		System.out.println();
	}
	/**
	 * Returns the document's path, or in a HTML document case, it's title. 
	 * 
	 * @return A String representation of the vector.
	 */
	public String toString(){
		return info.getTitle() == null ? info.getUrl() : info.getTitle();
	}

	/**
	 * Return the information of the vector.
	 * 
	 * @return A data structure that hold information about the vector.
	 */
	public WebsiteInformation getInfo() {
		return info;
	}

	/**
	 * Sets the information of the vector.
	 * 
	 * @param info - A data structure that hold information about the vector.
	 */
	public void setInfo(WebsiteInformation info) {
		this.info = info;
	}
	
}
